﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.Text;
using System.Threading.Tasks;

namespace WJP.DataPortal.Helper
{
    /// <summary>
    /// This class enables the user to configure the CompactMesssageEncoder with the configuration file.
    /// It's derived from ConfigurationElement
    /// </summary>
    public class CompactMessageEncodingElement : BindingElementExtensionElement
    {
        /// <summary>
        /// Stores the available properties of this ConfigurationElement.
        /// It's initialized in the Properties property.
        /// </summary>
        protected ConfigurationPropertyCollection _properties;

        /// <summary>Gets the type of the binding element.</summary>
        public override System.Type BindingElementType
        {
            get
            {
                return typeof(CompactMessageEncodingElement);
            }
        }

        /// <summary>Creates an instance of the binding element</summary>
        protected override BindingElement CreateBindingElement()
        {
            CompactMessageEncodingBindingElement encodingBindingElement = new CompactMessageEncodingBindingElement();
            this.ApplyConfiguration(encodingBindingElement);
            return encodingBindingElement;
        }

        /// <summary>Apply the configuration file to the binding element</summary>
        public override void ApplyConfiguration(BindingElement bindingElement)
        {
            CompactMessageEncodingBindingElement encodingBindingElement = (CompactMessageEncodingBindingElement)bindingElement;
            PropertyInformationCollection properties = this.ElementInformation.Properties;
            if (!this.BinaryMessageEncodingElement.ElementInformation.IsPresent)
                throw new ConfigurationErrorsException("An inner message encoder must be defined: 'binaryMessageEncoding'");
            if (!this.BinaryMessageEncodingElement.ElementInformation.IsPresent)
                return;
            encodingBindingElement.InnerBindingElement = (MessageEncodingBindingElement)new BinaryMessageEncodingBindingElement();
            this.BinaryMessageEncodingElement.ApplyConfiguration((BindingElement)encodingBindingElement.InnerBindingElement);
        }

        /// <summary>Gets the collection of properties.</summary>
        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                if (this._properties == null)
                {
                    ConfigurationPropertyCollection properties = base.Properties;
                    properties.Add(new ConfigurationProperty("binaryMessageEncoding", typeof(BinaryMessageEncodingElement), (object)null, (TypeConverter)null, (ConfigurationValidatorBase)null, ConfigurationPropertyOptions.None));
                    this._properties = properties;
                }
                return this._properties;
            }
        }

        /// <summary>Get the BinaryMessageEncodingElement configuration</summary>
        [ConfigurationProperty("binaryMessageEncoding")]
        public BinaryMessageEncodingElement BinaryMessageEncodingElement
        {
            get
            {
                return (BinaryMessageEncodingElement)this["binaryMessageEncoding"];
            }
        }
    }
}
